package com.trkj.taoruiyun;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.shamanland.fonticon.FontIconDrawable;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.utils.UserService;
import com.trkj.widget.refresh.SwipeRefreshLayout;

import java.text.MessageFormat;


/**
 * 资料下载
 * Created by LiZanhong on 2015/06/19.
 */
public class DocumentDownloadFragment extends BaseDataRefreshFragment
        implements ListView.OnItemLongClickListener{
    private int deptId;

    public int getDeptId() {
        return UserService.getInstance(getActivity()).parseUser().deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new DocumentLinkAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        getListView().setOnItemLongClickListener(this);
        return root;
    }

    @Override
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.DOCUMENT_LINK,
                getDeptId(),
                getPage(),
                getRows());
        Log.i("URL", url);
        return url;
    }

    @Override
    protected SwipeRefreshLayout.Mode getMode() {
        return SwipeRefreshLayout.Mode.BOTH;
    }

    /**
     * 单击选项打开链接
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String link = (String) parent.getAdapter().getItem(position);
        if(link != null){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(link));
            startActivity(intent);
        }
    }

    /**
     * 长按选项复制密码到剪切板
     * @param parent
     * @param view
     * @param position
     * @param id
     * @return
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        JSONArray data = getData();
        ClipboardManager clipboardManager =
                (ClipboardManager) getActivity().getSystemService(
                        Context.CLIPBOARD_SERVICE);
        String desc = data.getJSONObject(position).getString("link_desc");
        if(TextUtils.isEmpty(desc)){
            toast("没有内容需要复制到剪切板");
        }else {
            ClipData clipData = ClipData.newPlainText("描述信息已复制到剪切板", desc);
            clipboardManager.setPrimaryClip(clipData);
            toast("描述信息已复制到剪切板");
        }
        return true;
    }


    class DocumentLinkAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return getData().getJSONObject(position).getString("link_a");
        }

        @Override
        public long getItemId(int position) {
            return getData().getJSONObject(position).getIntValue("link_id");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if(convertView == null){
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_doc_item, null);
                viewHolder = new ViewHolder();
                viewHolder.linkName = (TextView) convertView.findViewById(R.id.link_name);
                viewHolder.linkDateIcon = (TextView) convertView.findViewById(R.id.link_date_icon);
                viewHolder.linkDate = (TextView) convertView.findViewById(R.id.link_date);
                viewHolder.linkDesc = (TextView) convertView.findViewById(R.id.link_desc);
                viewHolder.linkDescIcon = (TextView) convertView.findViewById(R.id.link_desc_icon);
                viewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.right_arrow);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }
            //设置箭头图案
            viewHolder.rightArrow.setImageDrawable(FontIconDrawable.inflate(getActivity(), R.xml.right_arrow));

            JSONArray data = getData();

            //设置描述
            String desc = data.getJSONObject(position).getString("link_desc");
            if(TextUtils.isEmpty(desc)){
                viewHolder.linkDescIcon.setVisibility(View.GONE);
                viewHolder.linkDesc.setVisibility(View.GONE);
            }else{
                viewHolder.linkDescIcon.setVisibility(View.VISIBLE);
                viewHolder.linkDesc.setVisibility(View.VISIBLE);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.linkDescIcon);
                viewHolder.linkDescIcon.setText(Constants.ResString.getString(getActivity(), R.string.fa_chain_broken));
                viewHolder.linkDesc.setText(desc);
            }

            //设置时间
            String date = data.getJSONObject(position).getString("link_date");
            if(TextUtils.isEmpty(date)){
                viewHolder.linkDateIcon.setVisibility(View.GONE);
                viewHolder.linkDate.setVisibility(View.GONE);
            }else{
                viewHolder.linkDateIcon.setVisibility(View.VISIBLE);
                viewHolder.linkDate.setVisibility(View.VISIBLE);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.linkDateIcon);
                viewHolder.linkDateIcon.setText(Constants.ResString.getString(getActivity(), R.string.fa_calendar_o));
                viewHolder.linkDate.setText(date);
            }

            //标题
            String title = data.getJSONObject(position).getString("link_title");

            if(data.getJSONObject(position).getBoolean("link_top")){
                viewHolder.linkName.setText("∧ " + title);
            }else{
                viewHolder.linkName.setText(title);
            }

            return convertView;
        }

        class ViewHolder{
            ImageView rightArrow;
            TextView linkName;
            TextView linkDescIcon;
            TextView linkDesc;
            TextView linkDateIcon;
            TextView linkDate;
        }
    }

}
